#!/usr/bin/env python
from typing import List
from fastapi import FastAPI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_ollama import ChatOllama
from langserve import add_routes
# 1. 创建提示模板
system_template = "Translate the following into {language}:"
prompt_template = ChatPromptTemplate.from_messages([
    ('system', system_template),
    ('user', '{text}')
])
# 2. 创建模型
model = ChatOllama(base_url="10.12.8.21:11434", model="qwen2.5:14b")
# 3. 创建解析器
parser = StrOutputParser()
# 4. 创建链
chain = prompt_template | model | parser
# 4. 应用程序定义
app = FastAPI(
  title="LangChain Server",
  version="1.0",
  description="A simple API server using LangChain's Runnable interfaces",
)
# 5. 添加链路由
add_routes(
    app,
    chain,
    path="/chain",
)
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="localhost", port=8000)